import React, { RefObject, useState } from 'react'
import { login } from "../../api/login.js"
import { useNavigate } from "react-router-dom";

import {
    Form,
    Input,
    Button,
    Dialog,
    TextArea,
    DatePicker,
    Selector,
    Slider,
    Stepper,
    Switch,
} from 'antd-mobile'

function Login() {
    let navigate = useNavigate()
    const [isRemenber, setIsRemenber] = useState(0);
    function switchCheck(e) {
        setIsRemenber(e);
    }

    const onFinish = async (values) => {
        console.log(values);
        const res = await login(values);
        console.log(res)
        if (res.data.msg == "success") {
            // if (isRemenber) {
            //     sessionStorage.setItem("isLogin", values.username)
            // }
            sessionStorage.setItem("isLogin", values.username)
            navigate("/wode")
        } else {
            sessionStorage.setItem("isLogin", null)
        }
    }

    return (
        <div>
            <Form
                layout='horizontal'
                onFinish={onFinish}
                footer={
                    <Button block type='submit' color='primary' size='large'>
                        提交
                    </Button>
                }
            >
                <Form.Header>用户登录</Form.Header>
                <Form.Item
                    name='username'
                    label='姓名'
                    rules={[{ required: true, message: '姓名不能为空' }]}
                >
                    <Input autoComplete='off' placeholder='请输入姓名' />
                </Form.Item>
                <Form.Item
                    name='password'
                    label='密码'
                    rules={[{ required: true, message: '密码不能为空' }]}
                >
                    <Input type='password' autoComplete='off' placeholder='请输入密码' />
                </Form.Item>


                <Form.Item
                    name='delivery'
                    label='记住密码'
                    childElementPosition='right'
                >
                    <Switch defaultChecked onChange={switchCheck} />
                </Form.Item>

            </Form>

        </div>
    );
}

export default Login;